package org.diffshop.product.controller;

import org.diffshop.common.utils.PageUtils;
import org.diffshop.common.utils.R;
import org.diffshop.product.entity.PmsAttrGroupEntity;
import org.diffshop.product.service.CategoryService;
import org.diffshop.product.service.PmsAttrAttrgroupRelationService;
import org.diffshop.product.service.PmsAttrGroupService;
import org.diffshop.product.vo.AttrAttrgroupRelationVO;
import org.diffshop.product.vo.AttrGroupWithAttrVO;
import org.diffshop.product.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author tjf
 * @date 2024-05-08 16:04:20
 */
@RestController
@RequestMapping("product/pmsattrgroup")
public class PmsAttrGroupController {
    @Autowired
    private PmsAttrGroupService pmsAttrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private PmsAttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 获取分类下的所有属性分组及关联属性(销售属性是没有属性分组的)
     * @param catalogId
     * @return
     */
    @GetMapping("/{catalogId}/withattr")
    public R queryAttrGroupWithAttr(@PathVariable("catalogId") Long catalogId){
        List<AttrGroupWithAttrVO> data = pmsAttrGroupService.getAttrGroupWithAttrByCatalogId(catalogId);

        return R.ok().put("data", data);
    }
    /**
     * 新建分组和属性关联
     * @param relationVO
     * @return
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody AttrAttrgroupRelationVO[] relationVO){
        attrAttrgroupRelationService.saveBatch(relationVO);

        return R.ok();
    }
    @DeleteMapping("/attr/relation/delete")
    public R deleteAttrAttrgroupRelation(@RequestBody AttrAttrgroupRelationVO[] relationVOs){
        attrAttrgroupRelationService.deleteAttrAttrgroupRelation(relationVOs);
        return R.ok();
    }
    /**
     * 获取当前属性分组没有关联的属性
     * @param params
     * @param attrGroupId
     * @return
     */
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R queryNoAttrsByAttrGroupId(@RequestParam Map<String, Object> params,
                                       @PathVariable Long attrGroupId){
        PageUtils page = pmsAttrGroupService.getNoAttrsByAttrGroupId(params, attrGroupId);

        return R.ok().put("page", page);
    }
    @GetMapping("/{attrGroupId}/attr/relation")
    public R queryAttrsByAttrGroupId(@PathVariable Long attrGroupId){
        List<AttrVO> attrVOList = pmsAttrGroupService.getAttrsByAttrGroupId(attrGroupId);
        return R.ok().put("data", attrVOList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = pmsAttrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 根据分类id获取属性分组
     * @param params
     * @param catId
     * @return
     */
    @GetMapping("/list/{catId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catId") Long catId){
        PageUtils page = pmsAttrGroupService.queryPage(params, catId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		PmsAttrGroupEntity pmsAttrGroup = pmsAttrGroupService.getById(attrGroupId);
        Long[] categoryPath = categoryService.getCategoryPath(pmsAttrGroup.getCatelogId());
        pmsAttrGroup.setCategoryPath(categoryPath);
        return R.ok().put("pmsAttrGroup", pmsAttrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PmsAttrGroupEntity pmsAttrGroup){
		pmsAttrGroupService.save(pmsAttrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PmsAttrGroupEntity pmsAttrGroup){
		pmsAttrGroupService.updateById(pmsAttrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		pmsAttrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
